/********************************************************************************************************
 * PROGRAM      : GSSGUI
 * DATE - TIME  : 
 * AUTHOR       :  ( Daniel Kish )
 * FILENAME     : gssview.cpp
 * LICENSE      : 
 * COMMENTARY   : 
 ********************************************************************************************************/
////////////////////////////////////////////////////////////////////
// added: when rack power is off, and the "abort" is selected at warning dialog, GUI is accesible again
////////////////////////////////////////////////////////////////////
#include "gssview.h"
#include <QtDebug>
#include <QSettings>
#include <QPushButton>
#include <QPixmap>
#include <QPalette>
#include <QDesktopWidget>
#include <QMessageBox>
#include "pstyledbutton.h"
//#include "camconportsettings.h"
//#include "servconportsettings.h"
//#include "frmpower.h"
#include "time.h"
#include "qhkp.h"

#include "gssglobals.h"
#include "hpsceventthread.h"
#include "ui_dlgmodal.h"
#include "dlgcamcon.h"
#include "dlgservcon.h"


#define DEBUG_ON    0
#define DEBUG if(DEBUG_ON)

///////////change log (new above)/////////
// added sanity checks for bus b,c selection (panel<->software)
//////////////////////////////////////////

// //HACKER PORT INPUT PORT A PIN ASIGNMENT
// #define UMBILICAL_CONN        PIN0
// #define SERVCON_SEP_CONN      PIN1
// #define REXUS_CONN            PIN2
// #define SERVICE_MOD_KEY       PIN3
// #define FLIGHT_SIM_KEY        PIN4
// #define FLIGHT_MODE_KEY       PIN5
// #define SWITCH_POS_B          PIN6
// #define SWITCH_POS_C          PIN7
// ////////////////////////////
// 
// //HACKER PORT INPUT PORT B PIN ASIGNMENT
// #define I_FLIGHT_SIM_SOE      PIN0
// #define I_FLIGHT_SIM_LO       PIN1
// #define I_FLIGHT_SIM_STAGE    PIN2
// #define I_FLIGHT_SIM_NCSEP    PIN3
// #define I_FLIGHT_SIM_YOYO_REL PIN4
// #define I_FLIGHT_SIM_CHUTE    PIN5
// #define POWER_RACK            PIN6
// ////////////////////////////
// 
// //HACKER PORT OUTPUT PORT A PIN ASIGNMENT
// #define CHAN_1                PIN0
// #define CHAN_2                PIN1
// #define CHAN_3                PIN2
// #define CHAN_4                PIN3
// #define BUS_B_SELECT          PIN6
// #define BUS_C_SELECT          PIN7
// ////////////////////////////
// 
// //HACKER PORT OUTPUT PORT B PIN ASIGNMENT
// #define O_FLIGHT_SIM_SOE      PIN0
// #define O_FLIGHT_SIM_LO       PIN1
// #define O_FLIGHT_SIM_STAGE    PIN2
// #define O_FLIGHT_SIM_NCSEP    PIN3
// #define O_FLIGHT_SIM_YOYO_REL PIN4
// #define O_FLIGHT_SIM_CHUTE    PIN5
// ////////////////////////////
#define HP_POLL_INTERVAL 250 //ms
#define HP_POLL_TIMEOUT  800 //ms

#define MAIN_DLG    0
#define CAMCON_DLG  1
#define SERVCON_DLG 2

GSSView::GSSView(/*quint16 port ,const QHostAddress & address,*/QWidget *parent)
    : QWidget(parent/*,Qt::FramelessWindowHint*/,Qt::Window),
 m_hpscEventThread(NULL),
 m_dlgCamCon(NULL),
 m_hkpTimer(NULL),
 m_iRackPowerState(-1),
 m_hkpResponded(true)
{
    
      
	ui.setupUi(this);
    QPixmap bgpixmap = QPixmap(":images/gssmain.png");
    QPalette p = this->palette();
    p.setBrush(this->backgroundRole(),QBrush(bgpixmap));
    this->setPalette(p);
    
    getHosts();
    QSettings settings("../../gss.conf",QSettings::IniFormat);
    QString hpscHost = settings.value("hosts/HPSC_IP"/*,"127.0.0.1"*/).toString();
    quint32 hpscPort = settings.value("hosts/HPSC_Port"/*,3010*/).toInt();
    DEBUG qDebug()<<"HPSC HOST:"<<hpscHost<<" port:"<<hpscPort;
    
    m_hpscEventThread = new HPSCEventThread(hpscHost,hpscPort);
    if(!m_hpscEventThread) qWarning()<<"could not allocate HPSCEventThread";
    //m_hpscEventThread->start();
    
    
    initControls();
	m_grpModules.addButton(ui.btnCamCon,0);
	m_grpModules.addButton(ui.btnServCon,1);
	m_grpModules.setExclusive(true);
    
    
    m_hkpTimer = new QTimer();
    m_hkpTimeout = new QTimer();
    connect(m_hkpTimer,SIGNAL(timeout()),this,SLOT(pollHkp()));
    connect(&m_grpModules,SIGNAL(buttonReleased(QAbstractButton *)),this,SLOT(releaseButtons()));
    connect(ui.btnCamCon,SIGNAL(released()),this,SLOT(selectCamCon()));
	connect(ui.btnServCon,SIGNAL(released()),this,SLOT(selectServCon()));
    connect (m_hkpTimeout,SIGNAL(timeout()),this,SLOT(hkpDisconnected()));
    
	
	getHosts();
    initSockets();
    connectToServers();
    m_hkpTimer->start(HP_POLL_INTERVAL);
    m_hkpTimeout->start(HP_POLL_TIMEOUT);
    QDesktopWidget desktop;
    if(desktop.isVirtualDesktop()) DEBUG qDebug()<<"virutal desktop screen:"<<desktop.screenNumber(this)<<" posx:"<<this->geometry().x()<<" screens:"<<desktop.numScreens();
    
    dlgSerialSettings = new DlgCommSettings(this);
    dlgSerialSettings->setWindowModality(Qt::ApplicationModal);
    dlgSerialSettings->move(this->geometry().x(),12);
    connect(dlgSerialSettings,SIGNAL(sig_hide()),m_dlgCamCon,SLOT(reloadSerialSettings()));
    
    connect(m_dlgCamCon,SIGNAL(sig_busStatus(FrmPower::BUS,FrmPower::POWER_STAT)),this,SLOT(updateBusStatus(FrmPower::BUS,FrmPower::POWER_STAT)));
    connect(m_dlgServCon,SIGNAL(sig_busStatus(FrmPower::BUS,FrmPower::POWER_STAT)),this,SLOT(updateBusStatus(FrmPower::BUS,FrmPower::POWER_STAT)));
    
}


GSSView::~GSSView()
{
  
}




/*!
    \fn GSSView::connectToServers()
 */
void GSSView::connectToServers()
{
    //TODO: check sockets and poort are valid
    qDebug()<<"GSSView::connectToServers():";
	//if(m_hpscSocket && m_hpscPort>0)
	//m_hpscSocket->connectToHost(m_hpscHost,m_hpscPort);
    if(m_hkpSocket)
        m_hkpSocket->connectToHost(m_hkpHost,m_hkpPort);
}


/*!
    \fn GSSView::getHosts()
 */
void GSSView::getHosts()
{
	QSettings settings("../../gss.conf",QSettings::IniFormat);
	m_hkpHost = settings.value("hosts/HKP_IP"/*,"127.0.0.1"*/).toString();
	m_hkpPort = settings.value("hosts/HKP_Port"/*,3010*/).toInt();
}


/*!
    \fn GSSView::initSockets()
 */
void GSSView::initSockets()
{
    m_hkpSocket = new QTcpSocket();
    connect(m_hkpSocket,SIGNAL(readyRead()),this,SLOT(hkpResponse()));
}


/*!
    \fn GSSView::selectFirmware
 */
void GSSView::selectFirmware()
{
    /// @todo implement me
}


/*!
    \fn GSSView::selectBatCharge()
 */
void GSSView::selectBatCharge()
{
    /// @todo implement me
}


/*!
    \fn GSSView::selectCamCon()
 */
void GSSView::selectCamCon()
{
//     //TODO: add active and pressed
 	ui.btnCamCon->setState(PStyledButton::on);
// 	ui.lblModulePort->setText("CamCon");
// 	ui.stkComm->setCurrentIndex(1);
 	ui.stkSectionMenu->setCurrentIndex(1);
	ui.stkModule->setCurrentIndex(1);
// 	ui.stkPower->setCurrentIndex(1);
}


/*!
    \fn GSSView::selectFlightSim()
 */
void GSSView::selectFlightSim()
{
    /// @todo implement me
}


/*!
    \fn GSSView::selectServCon()
 */
void GSSView::selectServCon()
{
 	ui.btnServCon->setState(PStyledButton::on);
// 	ui.lblModulePort->setText("ServCon");
    ui.stkModule->setCurrentIndex(2);
    ui.stkSectionMenu->setCurrentIndex(2);
}




/*!
    \fn GSSView::initControls()
 */
void GSSView::initControls()
{
    //Module CommPort
    m_dlgCamCon = new DlgCamCon(m_hpscEventThread);
    m_dlgServCon = new DlgServCon(m_hpscEventThread);
    ui.stkModule->insertWidget(CAMCON_DLG,m_dlgCamCon);
    ui.stkModule->insertWidget(SERVCON_DLG,m_dlgServCon);
    ui.stkModule->setCurrentIndex(MAIN_DLG);
    //Modules Selection menu
    ui.btnCamCon->replaceStatePixmap(PStyledButton::on_pressed,QPixmap(":images/modules/camcon/btn_camcon_active_pressed.png"));
    ui.btnCamCon->replaceStatePixmap(PStyledButton::on,QPixmap("images/modules/camcon/btn_camcon_active.png"));
    ui.btnCamCon->replaceStatePixmap(PStyledButton::on_mouseOver,QPixmap(":images/modules/camcon/btn_camcon_active_pressed.png"));
    ui.btnServCon->replaceStatePixmap(PStyledButton::on_pressed,QPixmap(":images/modules/servcon/btn_servcon_active_pressed.png"));
    ui.btnServCon->replaceStatePixmap(PStyledButton::on,QPixmap(":images/modules/servcon/btn_servcon_active.png"));
    ui.btnServCon->replaceStatePixmap(PStyledButton::on_mouseOver,QPixmap(":images/modules/servcon/btn_servcon_active_pressed.png"));
    //Module sub menus
    //----------CamCon menu-------------------------
    QWidget *temp = new QWidget();
    mnuCamCon.setupUi(temp);
    ui.stkSectionMenu->insertWidget(1,temp);
    int btnIndex = 0;
    m_grpCamConMenu.addButton(mnuCamCon.btnMain,btnIndex);
    ((PStyledButton*)m_grpCamConMenu.button(btnIndex))->replaceStatePixmap(PStyledButton::off_pressed,QPixmap(":images/modules/camcon/main/btn_main_inactive_pressed.png"));
    ((PStyledButton*)m_grpCamConMenu.button(btnIndex))->replaceStatePixmap(PStyledButton::on_pressed,QPixmap(":images/modules/camcon/main/btn_main_inactive_pressed.png"));
    ((PStyledButton*)m_grpCamConMenu.button(btnIndex))->replaceStatePixmap(PStyledButton::on,QPixmap(":images/modules/camcon/main/btn_main_active.png"));
    btnIndex++;;
    m_grpCamConMenu.addButton(mnuCamCon.btnCameras,btnIndex);
    ((PStyledButton*)m_grpCamConMenu.button(btnIndex))->replaceStatePixmap(PStyledButton::off_pressed,QPixmap(":images/modules/camcon/cameras/btn_cameras_inactive_pressed.png"));
    ((PStyledButton*)m_grpCamConMenu.button(btnIndex))->replaceStatePixmap(PStyledButton::on_pressed,QPixmap(":images/modules/camcon/cameras/btn_cameras_inactive_pressed.png"));
    ((PStyledButton*)m_grpCamConMenu.button(btnIndex))->replaceStatePixmap(PStyledButton::on,QPixmap(":images/modules/camcon/cameras/btn_cameras_active.png"));
    btnIndex++;
    m_grpCamConMenu.addButton(mnuCamCon.btnServos,btnIndex);
    ((PStyledButton*)m_grpCamConMenu.button(btnIndex))->replaceStatePixmap(PStyledButton::off_pressed,QPixmap(":images/modules/camcon/servos/btn_servo_motors_pressed.png"));
    ((PStyledButton*)m_grpCamConMenu.button(btnIndex))->replaceStatePixmap(PStyledButton::on_pressed,QPixmap(":images/modules/camcon/servos/btn_servo_motors_pressed.png"));
    ((PStyledButton*)m_grpCamConMenu.button(btnIndex))->replaceStatePixmap(PStyledButton::on,QPixmap(":images/modules/camcon/servos/btn_servo_motors_active.png"));
    m_grpCamConMenu.setExclusive(true);
    //-----------------------------------------------
    //----------------ServCon menu-------------------
    temp = new QWidget();
    mnuServCon.setupUi(temp);
    ui.stkSectionMenu->insertWidget(2,temp);
    btnIndex = 0;
    m_grpServConMenu.addButton(mnuServCon.btnMain,btnIndex);
    ((PStyledButton*)m_grpServConMenu.button(btnIndex))->replaceStatePixmap(PStyledButton::off_pressed,QPixmap(":images/modules/servcon/main/btn_main_inactive_pressed.png"));
    ((PStyledButton*)m_grpServConMenu.button(btnIndex))->replaceStatePixmap(PStyledButton::on_pressed,QPixmap(":images/modules/servcon/main/btn_main_active_pressed.png"));
    ((PStyledButton*)m_grpServConMenu.button(btnIndex))->replaceStatePixmap(PStyledButton::on,QPixmap(":images/modules/servcon/main/btn_main_active.png"));
    btnIndex++;
    m_grpServConMenu.addButton(mnuServCon.btnTm,btnIndex);
    ((PStyledButton*)m_grpServConMenu.button(btnIndex))->replaceStatePixmap(PStyledButton::off_pressed,QPixmap(":images/modules/servcon/TMTV/btn_tm_tv_pressed.png"));
    ((PStyledButton*)m_grpServConMenu.button(btnIndex))->replaceStatePixmap(PStyledButton::on_pressed,QPixmap(":images/modules/servcon/TMTV/btn_tm_tv_pressed.png"));
    ((PStyledButton*)m_grpServConMenu.button(btnIndex))->replaceStatePixmap(PStyledButton::on,QPixmap(":images/modules/servcon/TMTV/btn_tm_tv_active.png"));
    btnIndex++;
    m_grpServConMenu.addButton(mnuServCon.btnInflation,btnIndex);
    ((PStyledButton*)m_grpServConMenu.button(btnIndex))->replaceStatePixmap(PStyledButton::off_pressed,QPixmap(":images/modules/servcon/inflation/btn_inflation_system_pressed.png"));
    ((PStyledButton*)m_grpServConMenu.button(btnIndex))->replaceStatePixmap(PStyledButton::on_pressed,QPixmap(":images/modules/servcon/inflation/btn_inflation_system_pressed.png"));
    ((PStyledButton*)m_grpServConMenu.button(btnIndex))->replaceStatePixmap(PStyledButton::on,QPixmap(":images/modules/servcon/inflation/btn_inflation_system_active.png"));
    btnIndex++;
    m_grpServConMenu.addButton(mnuServCon.btnServos,btnIndex);
    ((PStyledButton*)m_grpServConMenu.button(btnIndex))->replaceStatePixmap(PStyledButton::off_pressed,QPixmap(":images/modules/servcon/servos/btn_servos_pressed.png"));
    ((PStyledButton*)m_grpServConMenu.button(btnIndex))->replaceStatePixmap(PStyledButton::on_pressed,QPixmap(":images/modules/servcon/servos/btn_servos_pressed.png"));
    ((PStyledButton*)m_grpServConMenu.button(btnIndex))->replaceStatePixmap(PStyledButton::on,QPixmap(":images/modules/servcon/servos/btn_servos_active.png"));
    
    
 	///////////CamCon connections////////////////////
    connect(&m_grpCamConMenu,SIGNAL(buttonReleased(int)),(DlgCamCon*)(ui.stkModule->widget(CAMCON_DLG)),SLOT(selectSection(int)));
    connect(&m_grpCamConMenu,SIGNAL(buttonReleased(QAbstractButton *)),this,SLOT(releaseButtons()));
    connect(m_dlgCamCon,SIGNAL(sig_serialSettings(MODULE)),this,SLOT(serialSettigs(MODULE)));
    connect(this,SIGNAL(sig_hkp(HkpCommand)),m_dlgCamCon,SIGNAL(sig_hkp(HkpCommand)));
    
    ///////////ServCon connections////////////////////
    connect(this,SIGNAL(sig_hkp(HkpCommand)),m_dlgServCon,SIGNAL(sig_hkp(HkpCommand)));
    connect(&m_grpServConMenu,SIGNAL(buttonReleased(int)),(DlgServCon*)(ui.stkModule->widget(SERVCON_DLG)),SLOT(selectSection(int)));
    connect(&m_grpServConMenu,SIGNAL(buttonReleased(QAbstractButton *)),this,SLOT(releaseButtons()));
}

/*!
    \fn GSSView::debug_int(int val)
 */
void GSSView::debug_int(int val)
{
    DEBUG qDebug()<<"debug_int"<<val;
}


/*!
    \fn GSSView::serialSettigs()
 */
void GSSView::serialSettigs(MODULE module)
{
  dlgSerialSettings->loadConfig(module);
  dlgSerialSettings->show();
  m_dlgCamCon->commSettigsDlg()->loadConfig(CAMCON);
}


/*!
    \fn GSSView::releaseButtons(QButtonGroup *btnGrp)
 */
void GSSView::releaseButtons()
{
    QList<QAbstractButton *> btnList = ((QButtonGroup*)(this->sender()))->buttons();
    QList<QAbstractButton *>::iterator it = btnList.begin();
    for(;it!=btnList.end();++it)
    {
        if(!(*it)->isChecked())
            ((PStyledButton*)(*it))->setState(PStyledButton::off);
    }
}


/*!
    \fn GSSView::pollPower()
 */
void GSSView::pollHkp()
{
    QDataStream hkpIn(m_hkpSocket);
    HkpCommand command;

    if(m_hkpSocket->state() == QAbstractSocket::ConnectedState)
    {
        //poll in port A
        command.type = READ_IN;
        command.mode = BYTE;
        command.port = QHkp::A;
        hkpIn<<command;

        //poll in port B
        command.type = READ_IN;
        command.mode = BYTE;
        command.port = QHkp::B;
        hkpIn<<command;
        
        //poll out port A
        command.type = READ_OUT;
        command.mode = BYTE;
        command.port = QHkp::A;
        hkpIn<<command;
        
        //poll out port B
        command.type = READ_OUT;
        command.mode = BYTE;
        command.port = QHkp::B;
        hkpIn<<command;
        
        m_hkpSocket->flush();
        
    }
}


/*!
    \fn GSSView::hkpResponse()
 */
void GSSView::hkpResponse()
{
    QDataStream hkpOut(m_hkpSocket);
    HkpCommand response;
    bool powerOn = false;
    m_hkpTimeout->stop();
    m_hkpTimer->stop();
    
    while(m_hkpSocket->bytesAvailable() >= sizeof(HkpCommand))
    {
        hkpOut>>response;
        
        if(response.port == QHkp::A && response.type == READ_IN )
        {
            if(response.data & SWITCH_POS_B && FrmPower::getBorC() != FrmPower::B)
            {
                //SWITCH_POS_B
                DEBUG qDebug()<<"SWITCH_POS_B";
                QMessageBox::warning(NULL,"Sanaty check failed: Power bus conflict","Bus B selected on panel , but in software!!!");
            }
                        
            if(response.data & SWITCH_POS_C && FrmPower::getBorC() != FrmPower::C)
            {
                //SWITCH_POS_C
                DEBUG qDebug()<<"SWITCH_POS_C";
                QMessageBox::warning(NULL,"Sanaty check failed: Power bus conflict","Bus C selected on panel , but in software!!!");
            }
            
            
            if(response.data & UMBILICAL_CONN)
            {
            //umbilical connected
                 qDebug()<<"UMBILICAL_CONN";
            }
            else {
                    //ubilical disconnected
            }
            if(response.data & SERVCON_SEP_CONN)
            {
                DEBUG qDebug()<<"SERVCON_SEP_CONN";
            }
            else {
                //CamCon disconnected
            }
            if(response.data & REXUS_CONN)
            {
                //REXUS connected
                //Disable ground power
                DEBUG qDebug()<<"REXUS_CONN";
            }
            else {
                    //REXUS disconnected
            }
            if(response.data & SERVICE_MOD_KEY)
            {
                DEBUG qDebug()<<"SERVICE_MOD_KEY";
            }
            else {
                    
            }
            if(response.data & FLIGHT_SIM_KEY)
            {
                //Flight Sim key
                DEBUG qDebug()<<"FLIGHT_SIM_KEY";
            }
            else {
                    ////Flight Sim key disconnected
            }
            if(response.data & FLIGHT_MODE_KEY)
            {
                //Flight mode key connected
                DEBUG qDebug()<<"FLIGHT_MODE_KEY";
            }
            else {
                    //Flight mode key disconnected
            }
            
            
        }
        
        if(response.port == QHkp::B && response.type == READ_IN)
        {
            if(response.data & I_FLIGHT_SIM_SOE )
            {
                DEBUG qDebug()<<"I_FLIGHT_SIM_SOE";
            }
            else {
                
            }
            if(response.data & I_FLIGHT_SIM_LO)
            {
                DEBUG qDebug()<<"I_FLIGHT_SIM_LO";
            }
            else {
            
            }
            if(response.data & I_FLIGHT_SIM_STAGE)
            {
                DEBUG qDebug()<<"I_FLIGHT_SIM_STAGE";
            }
            else {
    
            }
            if(response.data & I_FLIGHT_SIM_NCSEP)
            {
                DEBUG qDebug()<<"I_FLIGHT_SIM_NCSEP";
            }
            else {
    
            }
            if(response.data & I_FLIGHT_SIM_YOYO_REL)
            {
                DEBUG qDebug()<<"I_FLIGHT_SIM_YOYO_REL";
            }
            else {
    
            }
            if(response.data & I_FLIGHT_SIM_CHUTE)
            {
                DEBUG qDebug()<<"I_FLIGHT_SIM_CHUTE";
            }
            else {
    
            }
            
            
            if(response.data & POWER_RACK)
            {
                //Rack power off
                m_hkpTimer->stop();
                Ui::DlgModal msgDlg;
                QDialog dlg;
                QPixmap bgpixmap = QPixmap(":images/bkg_poweroff.png");
                QPalette p = dlg.palette();
                p.setBrush(dlg.backgroundRole(),QBrush(bgpixmap));
                dlg.setPalette(p);
                dlg.setWindowFlags(Qt::FramelessWindowHint);
                dlg.move(ui.stkModule->pos().x()+20,190);
                msgDlg.setupUi(&dlg);
                if(m_iRackPowerState != -1)
                if(dlg.exec() == QDialog::Accepted) m_hkpTimer->start(HP_POLL_INTERVAL);
                else {
                    DEBUG qDebug()<<"Aborted";
                    m_iRackPowerState = -1;
                }
            }
            else {
                    //Rack power on
                m_hkpTimer->start(HP_POLL_INTERVAL);
                m_hkpTimeout->start(HP_POLL_TIMEOUT);
                DEBUG qDebug()<<"power ON";
                m_iRackPowerState = 1;
            }
        }
        
        if(response.port == QHkp::A && response.type == READ_OUT)
        {
            //qDebug()<<"response READ_OUT A "<<response.data;;
            if(response.data & BUS_B_SELECT && FrmPower::getBorC() != FrmPower::B )
            {
               QMessageBox::warning(NULL,"Sanaty check failed: Power bus conflict","Bus B selected , but bus C is being sent on HP!!!");
            }
            
            if(response.data & BUS_C_SELECT && FrmPower::getBorC() != FrmPower::C )
            {
                QMessageBox::warning(NULL,"Sanaty check failed: Power bus conflict","Bus C selected , but bus B is being sent on HP!!!");
            }
            
        }
        
        if(response.port == QHkp::B && response.type == READ_OUT)
        {
            //qDebug()<<"response READ_OUT B "<<response.data;
            if(response.data & O_FLIGHT_SIM_SOE )
            {
                DEBUG qDebug()<<"O_FLIGHT_SIM_SOE";
            }
            else {
                
            }
            if(response.data & O_FLIGHT_SIM_LO)
            {
                DEBUG qDebug()<<"O_FLIGHT_SIM_LO";
            }
            else {
            
            }
            if(response.data & O_FLIGHT_SIM_STAGE)
            {
                DEBUG qDebug()<<"O_FLIGHT_SIM_STAGE";
            }
            else {
    
            }
            if(response.data & O_FLIGHT_SIM_NCSEP)
            {
                DEBUG qDebug()<<"O_FLIGHT_SIM_NCSEP";
            }
            else {
    
            }
            if(response.data & O_FLIGHT_SIM_YOYO_REL)
            {
                DEBUG qDebug()<<"O_FLIGHT_SIM_YOYO_REL";
            }
            else {
    
            }
            if(response.data & O_FLIGHT_SIM_CHUTE)
            {
                DEBUG qDebug()<<"O_FLIGHT_SIM_CHUTE";
            }
            else {
    
            }
        }
        
        emit sig_hkp(response);
    }
    m_hkpTimer->start(HP_POLL_INTERVAL);
    m_hkpTimeout->start(HP_POLL_TIMEOUT);
}


/*!
    \fn GSSView::hkpDisconnected()
 */
void GSSView::hkpDisconnected()
{
    qDebug()<<"GSSView::hkpDisconnected: connection to HkpServer lost - trying to reconnect...";
    m_hkpSocket->disconnectFromHost();
    m_hkpSocket->connectToHost(m_hkpHost,m_hkpPort);
}


/*!
    \fn GSSView::updateBusStatus(FrmPower::BUS bus, FrmPower::POWER_STAT status)
 */
void GSSView::updateBusStatus(FrmPower::BUS bus, FrmPower::POWER_STAT status)
{
    switch(bus)
    {
        case FrmPower::A:
//            qDebug()<<"A";
                setBusStatusLed(ui.lblLedA,status);
            break;
        case FrmPower::B:
 //           qDebug()<<"B";
                setBusStatusLed(ui.lblLedB,status);
            break;
        case FrmPower::C:
//            qDebug()<<"C";
                setBusStatusLed(ui.lblLedC,status);
            break;
        case FrmPower::D:
//            qDebug()<<"D";
                setBusStatusLed(ui.lblLedD,status);
            break;
    }
}




/*!
    \fn GSSView::setBusStatusLed(QLabel *led,FrmPower::POWER_STAT status)
 */
void GSSView::setBusStatusLed(QLabel *led,FrmPower::POWER_STAT status)
{
    QPalette p = led->palette();
    switch(status)
    {
        case FrmPower::POWER_OK:
            //qDebug()<<"POWER_OK";
            p.setBrush(led->backgroundRole(),QBrush(QColor(0,255,0)));
            led->setPalette(p);
            break;
        case FrmPower::OVERVOLT:
            //qDebug()<<"OVERVOLT";
            p.setBrush(led->backgroundRole(),QBrush(QColor(255,0,0)));
            led->setPalette(p);
            break;
        case FrmPower::UNDERVOLT:
            //qDebug()<<"UNDERVOLT";
            p.setBrush(led->backgroundRole(),QBrush(QColor(255,0,0)));
            led->setPalette(p);
            break;
        case FrmPower::AMPLIMIT:
            //qDebug()<<"AMPLIMIT";
            p.setBrush(led->backgroundRole(),QBrush(QColor(255,0,0)));
            led->setPalette(p);
            break;
        case FrmPower::POWER_OFF:
            //qDebug()<<"POWER_OFF";
            p.setBrush(led->backgroundRole(),QBrush(QColor(0,51,0)));
            led->setPalette(p);
            break;
    }
}
